import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { CateEntity } from 'src/cate/entities/cate.entity';
import { TextEntity } from 'src/text/entities/text.entity';

@Index('category', ['category'], {})
@Index('bookname', ['bookname'], {})
@Index('bookid', ['bookid'], {})
@Entity('book_books', { schema: 'db_book' })
export class BookEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('varchar', { name: 'bookname', nullable: true, length: 255 })
  bookname: string | null;

  @Column('varchar', { name: 'author', length: 255 })
  author: string;

  @Column('varchar', { name: 'category', nullable: true, length: 255 })
  category: string | null;

  @Column('text', { name: 'pic', nullable: true })
  pic: string | null;

  @Column('int', { name: 'bookid', nullable: true })
  bookid: number | null;

  @Column('int', { name: 'cateid', nullable: true })
  cateid: number | null;

  @Column('text', { name: 'blurb', nullable: true })
  blurb: string | null;

  @Column('varchar', { name: 'collect', nullable: true, length: 255 })
  collect: string | null;

  @Column('varchar', { name: 'hot', nullable: true, length: 255 })
  hot: string | null;

  @Column('varchar', { name: 'history', nullable: true, length: 255 })
  history: string | null;

  @ManyToOne(() => CateEntity, (cateEntity) => cateEntity.bookBooks, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'category', referencedColumnName: 'name' }])
  category2: CateEntity;

  @OneToMany(() => TextEntity, (textEntity) => textEntity.book)
  bookContents: TextEntity[];

  @OneToMany(() => TextEntity, (textEntity) => textEntity.bookname2)
  bookContents2: TextEntity[];
}
